<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<head>
	<meta charset="utf-8">
	<title>layui-demo</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="layui/css/layui.css" media="all">
	<link rel="stylesheet" href="font-awesome-4.7.0/css/font-awesome.css" />
	<!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body class="layui-layout-body">
	<div class="layui-layout layui-layout-admin">

		<div id="editFormDiv" style="display: none;padding : 20px 30px 0 0;">
			<form class="layui-form" action="" lay-filter="example">

				<div class="layui-form-item">
					<label class="layui-form-label">电影类型编号</label>
					<div class="layui-input-block">
						<input type="text" name="mtypeid" id="mtypeid" lay-verify="title" autocomplete="off" readonly class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">电影类型名</label>
					<div class="layui-input-block">
						<input type="text" name="mtypename" id="mtypename" lay-verify="title" autocomplete="off" placeholder="请输入电影类型名" class="layui-input">
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn btn-update" type="button" lay-submit="" lay-filter="demo1">立即提交</button>
					</div>
				</div>
			</form>
		</div>

		<div id="addFormDiv" style="display: none ;padding : 20px 30px 0 0;">
			<form class="layui-form" action="" lay-filter="example">
				<div class="layui-form-item">
					<label class="layui-form-label">电影类型</label>
					<div class="layui-input-block">
						<input type="text" name="amtypename" id="amtypename" lay-verify="title" autocomplete="off" placeholder="请输入类型名" class="layui-input">
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn btn-update" type="button" lay-submit="" lay-filter="demo2">立即提交</button>
					</div>
				</div>
			</form>
		</div>

		<div>
			<!-- 内容主体区域 -->
			<div style="padding: 15px;">
				<div class="demoTable">
					搜索：
					<div class="layui-inline">
						<input class="layui-input" name="id" id="demoReload" autocomplete="off">
					</div>
					<button class="layui-btn" data-type="reload">搜索</button>
				</div>
				<table class="layui-hide" id="test" lay-filter="test"></table>
			</div>
		</div>

	</div>


	<script type="text/html" id="toolbarDemo">
		<div class="layui-btn-container">
  			<button class="layui-btn layui-btn-sm" lay-event="DelMore">批量删除</button>
 			<button class="layui-btn layui-btn-sm" lay-event="AddBlog">添加电影类型</button>
 	 	</div>
	</script>

	<script type="text/html" id="barDemo">
		<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  		<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	</script>

	<script src="layui/layui.js" charset="utf-8"></script>
	<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

	<script>
		layui.use('table', function() {
			var table = layui.table;
			table.render({
				elem: '#test',
				url: 'MovieType', //地址和springmvc中控制器地址一致
				toolbar: '#toolbarDemo',
				title: '电影类型表',
				width: 1680,
			    height: 530,
				cols: [
					[{
							type: 'checkbox',
							fixed: 'left'
						}, {
							field: 'mtypeid',
							title: '电影类型编号',
							width: 530,
							fixed: 'left',
							unresize: true,
							sort: true
						}, {
							fixed: 'left',
							field: 'mtypename',
							title: '电影类型名称',
							width: 533
						}, {
							fixed: 'right',
							title: '操作',
							toolbar: '#barDemo',
							width: 550
						}
					]
				],
				page: true,
				id: 'testReload'
			});

			//头工具栏事件
			table.on('toolbar(test)', function(obj) {
				var checkStatus = table.checkStatus(obj.config.id);
				console.log(checkStatus);
				switch (obj.event) {
					case 'DelMore':
						layer.confirm('是否进行批量删除?',function(){
							var list = checkStatus.data
							var dellist = [];  
							$.each(list,function(i,o){
								console.log(o);
								dellist.push(o.mtypeid);
							})
							console.log("list:"+dellist);
							$.ajax({
								type : "DELETE",
								url : "delMoreType",
								data : JSON.stringify(dellist),
								contentType : "application/json",
								success : function(data){
									layer.msg(data.msg, {
										time: 2000
										//两秒延迟
									}, function() {
										if (data.code == 200) {
											location.replace(location.href);
										}
									});
									
								},
								dataType : "json"
								
								
							})
							
						});
						break;
					case 'AddBlog':
						console.log("add");
						layer.open({
							type: 1 //弹出层 div
								,
							area: ['400px', '300px'],
							shade: 0.6,
							id: 'LAY_layuipro1' //设定一个id，防止重复弹出
								,
							//content : $("#editFormDiv").html(),
							content: $("#addFormDiv"), //后面不用.html()

						});
				}
			});

			//监听行工具事件
			table.on('tool(test)', function(obj) {
				var data = obj.data;
				//console.log(obj)
				if (obj.event === 'del') {
					layer.confirm('真的删除该类型吗', function(index) {
						//console.log(data); //得到的是当前行对应的数据信息
						console.log("mtypeid :" + data.mtypeid);
						$.ajax({
							type: "DELETE", //提交方式
							url: "delMovieType/" + data.mtypeid, //地址和springmvc中控制器地址一致
							success: function(data) {
								layer.msg(data.msg, {
									time: 2000
									//两秒延迟
								}, function() {
									if (data.code == 200) {
										location.replace(location.href);
									}
								});
							}
						});
					});
				} else if (obj.event === 'edit') {

					//先给我们的表单元素赋值
					$("#mtypeid").attr("value",data.mtypeid);
					$("#mtypename").attr("value",data.mtypename);

					layer.open({
						type: 1 //弹出层 div
							,
						area: ['400px', '300px'],
						shade: 0.6,
						id: 'LAY_layuipro' //设定一个id，防止重复弹出
							,
						//content : $("#editFormDiv").html(),
						content: $("#editFormDiv"), //后面不用.html()

					});

					//表单的render
					layui.form.render();
				} else if(obj.event === 'DelMore'){
					console.log("click");
					console.log(data.mtypeid);
					
				}
			});

			/*表格数据重载 模糊检索*/
			var $ = layui.$,
				active = {
					reload: function() {
						var demoReload = $('#demoReload');
						//执行重载
						table.reload('testReload', {
							page: {
								curr: 1
								//重新从第 1 页开始
							},
							where: { //检索的条件
								key: demoReload.val()
							}
						});
					}
				};

			$('.demoTable .layui-btn').on('click', function() {
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			});

			//监听提交  新增加的代码  监听2
			layui.form.on('submit(demo1)', function(data) {
				//表单中的数据 进行转换，得到一个json格式的字符串
				console.log(JSON.stringify(data.field));
				//ajax请求实现访问控制器
				$.ajax({
					type: "post", //提交方式
					url: "updMovieType", ////地址和springmvc中控制器地址一致
					data: JSON.stringify(data.field), //提交给控制器的数据
					contentType: "application/json", //提交给控制的数据 格式
					success: function(data) {
						layer.msg(data.msg, {
							time: 2000
							//两秒延迟
						}, function() {
							if (data.code == 200) {
								location.replace(location.href);
							}

						});

					}
				});

				return false;
			});

			layui.form.on('submit(demo2)', function(data) {
				var mt = new Object;
				mt.mtypename = $("#amtypename").val();
				console.log(mt);
				 $.ajax({
					type: "post",
					url: "addMovieType",
					data: JSON.stringify(mt),
					contentType: "application/json",
					success: function(data) {
						if (data.code == 200) {
							layer.msg(data.msg);
						     setTimeout(function(){
						    	 location.replace(location.href);
				                },1000);
						}else{
							layer.msg(data.msg);
						}
					},
					dataType : "json"

				}); 

			});



		});
	</script>


	<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>

	<script>
		//JavaScript代码区域
		layui.use('element', function() {
			var element = layui.element;

		});
		var isShow = true; //定义一个标志位
		$('.kit-side-fold').click(function() {
			//选择出所有的span，并判断是不是hidden
			$('.layui-nav-item span').each(function() {
				if ($(this).is(':hidden')) {
					$(this).show();
				} else {
					$(this).hide();
				}
			});
			//判断isshow的状态
			if (isShow) {
				$('.layui-side.layui-bg-black').width(60); //设置宽度
				$('.kit-side-fold i').css('margin-right', '70%'); //修改图标的位置
				//将footer和body的宽度修改
				$('.layui-body').css('left', 60 + 'px');
				$('.layui-footer').css('left', 60 + 'px');
				//将二级导航栏隐藏
				$('dd span').each(function() {
					$(this).hide();
				});
				//修改标志位
				isShow = false;
			} else {
				$('.layui-side.layui-bg-black').width(200);
				$('.kit-side-fold i').css('margin-right', '10%');
				$('.layui-body').css('left', 200 + 'px');
				$('.layui-footer').css('left', 200 + 'px');
				$('dd span').each(function() {
					$(this).show();
				});
				isShow = true;
			}
		});
	</script>

</body>
</html>
